﻿Imports System.Data
Imports System.Data.SqlClient
Imports Entidad

Public Class NotaIngresoDatos

    Sub registrarNotaIngreso(ByVal ni As NotaIngreso)
        Dim conex As New Conexion
        Dim coneccion As SqlConnection = conex.conectar()
        coneccion.Open()
        Dim query As String = "INSERT INTO Documento_Almacen(CIA,SUC_Codigo,TDOC_Codigo,DCA_Codigo,DCA_FechaDocumento,EST_Codigo) " &
                              "VALUES('01','01','4',@dcaCodigo,@dafechadoc,'02')"
        Dim comando As New SqlCommand
        With comando
            .Connection = coneccion
            .CommandText = query
            .Parameters.AddWithValue("@dcaCodigo", ni.DCA_Codigo)
            .Parameters.AddWithValue("@dafechadoc", ni.DA_Fecha_Doc)
            .ExecuteNonQuery()
        End With
        coneccion.Close()
    End Sub

    Sub anularNotaIngreso(ByVal dcaCodigo As String)
        Dim conex As New Conexion
        Dim coneccion As SqlConnection = conex.conectar()
        coneccion.Open()
        Dim query As String = "Update Documento_Almacen set EST_Codigo = '17' " &
                              "where DCA_Codigo = @dcacodigo"
        Dim comando As New SqlCommand
        With comando
            .Connection = coneccion
            .CommandText = query
            .Parameters.AddWithValue("@dcacodigo", dcaCodigo)
            .ExecuteNonQuery()
        End With
        coneccion.Close()
    End Sub

    Function verificarNotaIngresoActiva(ByVal codDocumento As String, ByVal tipoDoc As String) As Boolean
        Dim valor As Boolean = False
        Dim conex As New Conexion
        Dim coneccion As SqlConnection = conex.conectar()
        coneccion.Open()
        Dim query As String = "select * from Documento_Cliente_Almacen_Detalle where DCI_Codigo_Cli = '" & codDocumento & "' and " &
                              "TDOC_Cli = '" & tipoDoc & "' and DCAD_Est <> '17'"
        Dim adapter As New SqlDataAdapter(query, coneccion)
        Dim ds As New DataSet
        adapter.Fill(ds)
        coneccion.Close()

        If ds.Tables(0).Rows.Count <> 0 Then
            valor = True
        End If

        Return valor
    End Function

    Sub anularNotaIngreso(ByVal tdoc As String, ByVal codDoc As String)
        Dim conex As New Conexion
        Dim coneccion As SqlConnection = conex.conectar()
        coneccion.Open()
        Dim query As String = "UPDATE Documento_Almacen " &
                              "SET EST_Codigo = '17' " &
                              "WHERE TDOC_Codigo = @tdoc' AND DCA_Codigo = @doc"
        Dim comando As New SqlCommand
        With comando
            .Connection = coneccion
            .CommandText = query
            .Parameters.AddWithValue("@tdoc", tdoc)
            .Parameters.AddWithValue("@doc", codDoc)
            .ExecuteNonQuery()
        End With
        coneccion.Close()
    End Sub

End Class
